Skip to content

Conversation

BenFordTytherington
Copy link
Collaborator

A trait which will wrap the QObjectExt methods for types which upcast to QObject. When used in conjunction with #1252 and #1226, this will allow any type that inherits from QObject - even if this is through many levels of inheritance - to have an as_qobject method, and if the trait is imported, expose the methods of QObject directly on that child class. Closes #562.

Copy link

codecov bot commented May 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (48acbdb) to head (ca22759).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1262   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           75        75           
  Lines        12764     12784   +20     
=========================================
+ Hits         12764     12784   +20     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@BenFordTytherington BenFordTytherington force-pushed the add-qtobject-trait branch 2 times, most recently from 913c91b to 3f749ed Compare May 20, 2025 12:09
@BenFordTytherington BenFordTytherington force-pushed the add-qtobject-trait branch 2 times, most recently from 9ed7e49 to c3061e8 Compare May 20, 2025 12:51
@BenFordTytherington BenFordTytherington requested review from LeonMatthesKDAB and ahayzen-kdab and removed request for jnbooth May 20, 2025 14:25
@LeonMatthesKDAB
Copy link
Collaborator

LeonMatthesKDAB commented May 20, 2025

@BenFordTytherington Didn't we at some point discuss just directly implementing QObjectExt for anything that is Upcast<QObject>, e.g.:

impl QObjectExt for T where T: Upcast<QObject> {
}

That way we don't need another trait 🤔

We should then however make sure to move all methods from QObject into QObjectExt (even dump_object_info)

@BenFordTytherington
Copy link
Collaborator Author

Just attempted to implement it for all these upcasting types for a bit, and the main problem is that now QObject upcasts to QObject, so we get a conflicting impl. I think this is initially why we went for a separate trait, and a blanket impl there, since my_obj.as_qobject().method() isn't too bad, compared to being able to do my_obj.method()

@LeonMatthesKDAB
Copy link
Collaborator

Why would we get a conflicting impl?
We no longer need to manually impl QObjectExt for QObject, as it will impl QObjectExt it via the auto-trait impl, right?

@BenFordTytherington BenFordTytherington force-pushed the add-qtobject-trait branch 2 times, most recently from 03f6cb8 to b010ed1 Compare June 17, 2025 12:44
@@ -64,12 +67,10 @@ pub trait QObjectExt {
fn object_name(&self) -> QString;

/// Returns a mutable pointer to the parent object.
fn parent(&self) -> *mut Self;
fn parent(&self) -> *mut QObjectExternal;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if this should be a QObject not external? As i might want to ask for the parent and then downcast from QObject* to a T* ? Guess we'd need to cast the QObjectExternal -> QObject in the parent method?

Or do we think that we do not need that and QObjectExternal is fine? 🤔

@@ -35,6 +35,9 @@ pub mod ffi {

#[rust_name = "set_parent"]
pub unsafe fn setParent(self: Pin<&mut Self>, parent: *mut QObjectExternal);

#[rust_name = "dump_object_info"]
fn dumpObjectInfo(&self);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be pub otherwise things will complain about it being unused :-)

- All objects which upcast to QObject now have QObjectExt implemented
- Via transitive casting and using the QObjectExt trait, any descendent of QObject can access its methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Traits for CxxQtType/QtObject
4 participants